//
//  SearchViewController.m
//  MXClassroom
//
//  Created by 你个LB on 16/9/12.
//  Copyright © 2016年 你个LB. All rights reserved.
//

#import "SearchViewController.h"

@interface SearchViewController ()

@end

@implementation SearchViewController

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    

}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    // 清除返回按钮
    UIBarButtonItem * backItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:nil];
    self.navigationItem.leftBarButtonItem = backItem;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 创建搜索框
    [self initSearchBarView];
    
    // 创建历史搜索试图
    [self initHistorySearchView];
    
    // 创建热门搜索试图
    [self initPopularSearchView];
}

// 创建搜索视图
- (void)initSearchBarView{
    
    // 创建取消按钮
    _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _cancelBtn.frame = CGRectMake(kScreen_width - 40 - 10, 0, 40, 40);
    [_cancelBtn setTitle:@"清除" forState:UIControlStateNormal];
    [_cancelBtn addTarget:self action:@selector(cancelBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [_cancelBtn setTitleColor:kColor_title_btn_color forState:UIControlStateNormal];
    _cancelBtn.titleLabel.font = [UIFont systemFontOfSize:13 weight:2];
    [self.navigationController.navigationBar addSubview:_cancelBtn];
    
    // 创建搜索视图
    _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(10, 0, kScreen_width - 60, 40)];
    _searchBar.delegate = self;
    _searchBar.placeholder = @"输入搜索关键字";
    _searchBar.searchBarStyle = UISearchBarStyleDefault;
    [self.navigationController.navigationBar addSubview:_searchBar];
}

// 创建历史搜索试图
- (void)initHistorySearchView{
    // 历史搜索背景试图
    _historySearchBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, kScreen_width, 90)];
    [self.view addSubview:_historySearchBgView];
    
    // 小图标
    UIImageView * iconView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 17, 17)];
    iconView.image = [UIImage imageNamed:@"搜索列表图标34px.png"];
    [_historySearchBgView addSubview:iconView];
    
    // 历史搜索文本
    UILabel * messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(iconView.right + 8, 10, 60, 17)];
    messageLabel.text = @"历史搜索";
    messageLabel.textColor = kColor_title_btn_color;
    messageLabel.font = [UIFont boldSystemFontOfSize:14];
    [_historySearchBgView addSubview:messageLabel];
    
    NSArray *array = @[@"历史搜索1",@"历史搜索2",@"历史搜索3"];
    IMJIETagFrame *frame = [[IMJIETagFrame alloc] init];
    frame.tagsMinPadding = 4;
    frame.tagsMargin = 6;
    frame.tagsLineSpacing = 6;
    frame.tagsArray = array;
    
    IMJIETagView *tagView = [[IMJIETagView alloc] initWithFrame:CGRectMake(0, 30, SCREEN_WIDTH, frame.tagsHeight)];
    tagView.clickbool = YES;
    tagView.borderSize = 0.5;
    tagView.clickborderSize = 0.5;
    tagView.tagsFrame = frame;
//    tagView.clickBackgroundColor = BACKGROUNDCOLOR;
//    tagView.clickTitleColor = BACKGROUNDCOLOR;
    tagView.clickStart = 0;
    tagView.clickString = @"华语";//单选  tagView.clickStart 为0
    tagView.delegate = self;
    [_historySearchBgView addSubview:tagView];
}

// 创建热门搜索试图
- (void)initPopularSearchView{
    // 历史搜索背景试图
    _popularSearchBgView = [[UIView alloc] initWithFrame:CGRectMake(0, _historySearchBgView.bottom + 8, kScreen_width, 90)];
    [self.view addSubview:_popularSearchBgView];
    
    // 小图标
    UIImageView * iconView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 17, 17)];
    iconView.image = [UIImage imageNamed:@"搜索列表图标34px.png"];
    [_popularSearchBgView addSubview:iconView];
    
    // 历史搜索文本
    UILabel * messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(iconView.right + 8, 10, 60, 17)];
    messageLabel.text = @"热门搜索";
    messageLabel.textColor = kColor_title_btn_color;
    messageLabel.font = [UIFont boldSystemFontOfSize:14];
    [_popularSearchBgView addSubview:messageLabel];
    
    NSArray *array = @[@"热门搜索1",@"热门搜索2",@"热门搜索3"];
    IMJIETagFrame *frame = [[IMJIETagFrame alloc] init];
    frame.tagsMinPadding = 4;
    frame.tagsMargin = 6;
    frame.tagsLineSpacing = 6;
    frame.tagsArray = array;
    
    IMJIETagView *tagView = [[IMJIETagView alloc] initWithFrame:CGRectMake(0, 30, SCREEN_WIDTH, frame.tagsHeight)];
    tagView.clickbool = YES;
    tagView.borderSize = 0.5;
    tagView.clickborderSize = 0.5;
    tagView.tagsFrame = frame;
    //    tagView.clickBackgroundColor = BACKGROUNDCOLOR;
    //    tagView.clickTitleColor = BACKGROUNDCOLOR;
    tagView.clickStart = 0;
    tagView.clickString = @"华语";//单选  tagView.clickStart 为0
    tagView.delegate = self;
    [_popularSearchBgView addSubview:tagView];
}

#pragma mark - 标签选中
//-(void)IMJIETagView:(NSArray *)tagArray{
//    
//    NSLog(@"%@",tagArray);
//}

- (void)IMJIETagView:(UIView *)view WithSelectTag:(NSInteger)tag{
    NSLog(@"view:%@,tag:%ld",view, tag);
}

// 取消按钮点击事件
- (void)cancelBtnAction{
    _searchBar.text = nil;
    [_searchBar resignFirstResponder];
    [_searchBar removeFromSuperview];
    [_cancelBtn removeFromSuperview];
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - searchBar代理方法
// 开始编辑时调用
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
    NSLog(@"开始编辑");
    [searchBar setShowsCancelButton:NO animated:YES];
}

// 结束编辑时调用
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
    NSLog(@"结束编辑");
    [searchBar setShowsCancelButton:NO animated:YES];
}

// 当搜索框内文字的内容发生改变时调用
- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    return YES;
}

// 取消按钮点击事件
//- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
//    searchBar.text = nil;
//    [searchBar resignFirstResponder];
//    [self.navigationController popViewControllerAnimated:YES];
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
